package dujiu.leetcode.slidingWindow.fixed_lengthSlidingWindow;

import java.util.Arrays;

public class P2090 {
    public int[] getAverages(int[] nums, int k) {
        int n = nums.length;
        int[] avg = new int[n];
        long sum =0 ;
        int size = 2*k+1;
        if (k == 0){
            return nums;
        }
        Arrays.fill(avg,-1);

        if (n < size) {
            return avg;
        }
        for (int j = 0; j < size; j++) {
            sum += nums[j];
        }
        avg[k] = (int) (sum/size);
        for (int i = size; i < nums.length; i++) {
            sum = sum - nums[i-size]+nums[i];
            avg[i-k] =(int)(sum/size);
        }
        return avg;
    }

    public static void main(String[] args) {
        int[] nums = {7,4,3,9,1,8,5,2,6};
        P2090 p = new P2090();
        System.out.println(Arrays.toString(p.getAverages(nums, 3)));
    }
}
